package com.study.view.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.Calendar;

import androidx.annotation.Nullable;

/**
 * author : xujianhong
 * date   : 2023/2/15
 * desc   : 自定义表盘
 * version: 1.0
 */
public class BlockView extends View {

    //定义圆圈画笔
    Paint circlePaint;
    //刻度画笔
    Paint scalePaint;
    //画文字画笔
    Paint txtPaint;
    //画秒针
    Paint secondPaint;
    //画分针
    Paint minutePaint;
    //画时针
    Paint hourPaint;
    //圆弧的画笔宽度
    private final float CIRCLE_WIDTH_SIZE = 5;

    //控件的中心点距离
    float centerWidget = 0;
    //短刻度的长度
    private final float MINUTE_LENTH = 15;

    //长刻度的长度
    private final float HOUR_LENTH = 30;
    //秒针的长度
    private final float SECOND_LINE_SIZE = 230;

    //画分针长度
    private final float MINUTE_LINE_SIZE = 180;

    //画时针长度
    private final float HOUR_LINE_SIZE = 120;

    //旋转秒针的角度
    private int secondAngle = 0;
    //分钟旋转的角度
    private int minuteAngle = 0;
    //小时旋转的角度
    private int hourAngle = 0;


    public BlockView(Context context) {
        this(context,null);
    }

    public BlockView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public BlockView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        circlePaint = initPaint();
        circlePaint.setStrokeWidth(CIRCLE_WIDTH_SIZE);

        scalePaint = initPaint();
        scalePaint.setStrokeWidth(CIRCLE_WIDTH_SIZE);

        txtPaint = initPaint();
        txtPaint.setStrokeWidth(CIRCLE_WIDTH_SIZE);
        txtPaint.setTextSize(40);

        secondPaint = initPaint();
        secondPaint.setStrokeWidth(CIRCLE_WIDTH_SIZE);

        minutePaint = initPaint();
        minutePaint.setStrokeWidth(10);


        hourPaint = initPaint();
        hourPaint.setStrokeWidth(15);
    }

    private Paint initPaint(){
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeCap(Paint.Cap.ROUND);
        return paint;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int centerSize = Math.min(width/2,height/2);

        //当前控件的中心位置距离
        centerWidget = centerSize/2;
        setMeasuredDimension(centerSize,centerSize);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        onDrawCircle(canvas);
        onDrawScale(canvas);
        onDrawText(canvas);
        onDrawSecond(canvas);
        onDrawMinute(canvas);
        onDrawHour(canvas);
    }

    /**
     * 画圆圈
     * @param canvas
     */
    private void onDrawCircle(Canvas canvas){
        float left = CIRCLE_WIDTH_SIZE/2;
        float top = CIRCLE_WIDTH_SIZE / 2;
        float right = getWidth() - CIRCLE_WIDTH_SIZE/2;
        float bottom = getHeight() - CIRCLE_WIDTH_SIZE/2;
        canvas.drawArc( left,  top,  right,  bottom,  0,
       360, false, circlePaint);
    }

    /**
     * 画刻度
     * @param canvas
     */
    private void onDrawScale(Canvas canvas){
        canvas.save();

        for (int i = 0; i < 360; i++) {
            float startX = CIRCLE_WIDTH_SIZE / 2;
            float startY = centerWidget;
            float stopX = CIRCLE_WIDTH_SIZE / 2 + MINUTE_LENTH;
            float stopY = centerWidget;

            if(i % 15 == 0){
                canvas.drawLine(startX, startY, stopX+HOUR_LENTH, stopY,
                        scalePaint);
            }else{
                canvas.drawLine(startX, startY, stopX, stopY,
                        scalePaint);
            }

            canvas.rotate(10,centerWidget, centerWidget);
        }
        canvas.restore();
    }

    /**
     * 画文字
     * @param canvas
     */
    private void onDrawText(Canvas canvas){
        canvas.save();
        //当前绘制的方向是
        int num = 6;
        for (int i = 0; i < 360; i++) {
            if(i % 30 == 0){
                if(num <= 0){
                    num = 12;
                }
                String time = String.valueOf(Math.abs(num));

                float Y = (float) (centerWidget + Math.cos(i * (Math.PI / 180)) * (centerWidget-80));
                float X = (float) (centerWidget + Math.sin(i * (Math.PI / 180)) * (centerWidget-80));

                Rect rect = new Rect();
                txtPaint.getTextBounds(time,0,time.length(),rect);
                float dx = X-rect.width()/2;
                // 获取基线
                Paint.FontMetrics fontMetrics = txtPaint.getFontMetrics();
                int dy = (int) ((fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom);
                int baseLine = (int) (Y + dy);

                canvas.drawText( time,  dx,  baseLine,txtPaint);
                num--;
            }

        }
        canvas.restore();
    }

    /**
     * 画秒针
     * @param canvas
     */
    private void onDrawSecond(Canvas canvas){
        canvas.save();
        canvas.rotate(secondAngle,centerWidget, centerWidget);
        canvas.drawLine(centerWidget, centerWidget-SECOND_LINE_SIZE, centerWidget, centerWidget,
        secondPaint);
        canvas.restore();
    }

    /**
     * 画分针
     * @param canvas
     */
    private void onDrawMinute(Canvas canvas){
        canvas.save();
        canvas.rotate(minuteAngle,centerWidget, centerWidget);
        canvas.drawLine(centerWidget, centerWidget-MINUTE_LINE_SIZE, centerWidget, centerWidget,
                minutePaint);
        canvas.restore();
    }

    /**
     * 画时针
     * @param canvas
     */
    private void onDrawHour(Canvas canvas){
        canvas.save();
        canvas.rotate(hourAngle,centerWidget, centerWidget);
        canvas.drawLine(centerWidget, centerWidget-HOUR_LINE_SIZE, centerWidget, centerWidget,
                hourPaint);
        canvas.restore();
    }

    public void startBlock(){

        //此处时间算法有问题
        Calendar calendar = Calendar.getInstance();
        float milliSecond = calendar.get(Calendar.MILLISECOND);
        float second = calendar.get(Calendar.SECOND) + milliSecond / 1000;
        float minute = calendar.get(Calendar.MINUTE) + second / 60;
        float hour   = calendar.get(Calendar.HOUR)   + minute / 60;
        secondAngle = (int) (second / 60 * 360);
        minuteAngle = (int) (minute / 60 * 360);
        hourAngle   = (int) (hour   *(360 * 12));


        Log.d(TAG, "startBlock: "+hour);
        invalidate();
    }

    private static final String TAG = "BlockView";
}
